package com.quarkboom.kotlinapp

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.TelephonyManager
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
import java.util.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var i = false;
        button.setOnClickListener{
            i = !i;
//            while (i){
            println("eeeeeeeeee 1 =  "+ isNowOutOfTimeAcrossDay("12-1"))
            println("eeeeeeeeee 2 =  "+ isNowOutOfTimeAcrossDay("19-1"))
            println("eeeeeeeeee 2 =  "+ isNowOutOfTimeAcrossDay("22-1"))
            println("eeeeeeeeee 3 =  "+ isNowOutOfTimeAcrossDay("1-12"))
            println("eeeeeeeeee 4 =  "+ isNowOutOfTimeAcrossDay("12-23"))
                println("eeeeee hasSimCard = "+hasSimCard())
                println("eeeeee checkWifiIsEnable = "+checkWifiIsEnable())
                println("eeeeee isNetSystemUsable = "+isNetSystemUsable())
//                Thread.sleep(200)
//            }

        }
    }

    fun isNowOutOfTimeAcrossDay(time: String): Boolean {
        // 是否是检查推送时间
        return try {
            val times = time.split("-").toTypedArray()
            // 开始时间
            val start = Date()
            start.hours = times[0].toInt()
            start.minutes = 0
            start.seconds = 0
            // 结束时间
            val end = Date()
            end.hours = times[1].toInt()
            end.minutes = 0
            end.seconds = 0
            // 此时此刻
            val now = Date()


            if (times[0].toInt() > times[1].toInt()) {
                !(now.after(start) || now.before(end))
            } else {
                now.before(start) || now.after(end)
            }
        } catch (e: Exception) {
            true
        }
    }



    /**
     * 判断是否包含SIM卡
     *
     * @return 状态
     */
    fun hasSimCard(): Boolean {
        val telMgr = getApplication().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val simState = telMgr.simState
        var result = true
        when (simState) {
            TelephonyManager.SIM_STATE_ABSENT -> result = false // 没有SIM卡
            TelephonyManager.SIM_STATE_UNKNOWN -> result = false
        }
        return result
    }


    /**
     * 判断当前网络是否可用(6.0以上版本)
     * 实时
     * @param context
     * @return
     */
    fun isNetSystemUsable(): Boolean {
        var isNetUsable = false
        val manager = getApplication().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = manager.getNetworkCapabilities(manager.activeNetwork)
            if (networkCapabilities != null) {
                isNetUsable = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
            }
        }
        return isNetUsable
    }

    /**
     * 检查wifi是否可用
     */
    fun checkWifiIsEnable(): Boolean {
        val wifiManager = getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
        return null != wifiManager && wifiManager.isWifiEnabled
    }
}